package cn.licoy.wdog.core.service.hospital2.impl;

import cn.licoy.wdog.common.bean.ResponseCode;
import cn.licoy.wdog.common.exception.RequestException;
import cn.licoy.wdog.common.util.Encrypt;
import cn.licoy.wdog.common.util.Tools;
import cn.licoy.wdog.core.config.jwt.JwtToken;
import cn.licoy.wdog.core.dto.hospital2.doctor.CertificationDto;
import cn.licoy.wdog.core.entity.hospital.HospitalUser;
import cn.licoy.wdog.core.entity.hospital2.Certification;
import cn.licoy.wdog.core.entity.hospital2.Doctor;
import cn.licoy.wdog.core.entity.system.SysUser;
import cn.licoy.wdog.core.mapper.hospital2.DoctorMapper;
import cn.licoy.wdog.core.service.hospital2.DoctorService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author Sunne
 * @Date $time$ $date$
 * @Param $param$
 * @return $return$
 **/
@Service
@Transactional
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {


    @Override
    public Doctor registry(Doctor doctor) {
        Doctor dt = this.selectOne(new EntityWrapper<Doctor>().eq("phone",doctor.getPhone()));
        if (dt!=null){
            throw RequestException.fail(
                    String.format("已经存在手机号为 %s 的用户",doctor.getPhone()));
        }
        dt = new Doctor();
        //BeanUtils.copyProperties(doctor,dt);
        doctor.setCreateDate(new Date());
        doctor.setStatus(1);
        doctor.setPasswd(Encrypt.md5(String.valueOf(doctor.getPasswd())+doctor.getPhone()));
        this.insert(doctor);
        JwtToken token = new JwtToken(null,doctor.getPhone(),doctor.getPasswd());
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            if(!subject.isAuthenticated()){
                throw new RequestException(ResponseCode.SIGN_IN_INPUT_FAIL);
            }
            Doctor ndt = this.selectOne(new EntityWrapper<Doctor>()
                    .eq("phone",doctor.getPhone())
                    .eq("passwd",Encrypt.md5(String.valueOf(doctor.getPhone()+doctor.getPasswd())))
            );
            return ndt;
        }catch (DisabledAccountException e){
            throw new RequestException(ResponseCode.SIGN_IN_INPUT_FAIL.code,e.getMessage(),e);
        }catch (Exception e){
            throw new RequestException(ResponseCode.SIGN_IN_FAIL,e);
        }
    }

    @Override
    public Doctor getCurrentUser() {
        HttpServletRequest request =
                ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        boolean b = Tools.executeLogin(request);
        if(!b){
            throw RequestException.fail("身份已过期或无效，请重新认证");
        }
        Subject subject = SecurityUtils.getSubject();
        if(!subject.isAuthenticated()){
            throw new RequestException(ResponseCode.NOT_SING_IN);
        }
        JwtToken jwtToken = new JwtToken();
        Object principal = subject.getPrincipal();
        if(principal==null){
            throw RequestException.fail("用户信息获取失败");
        }
        BeanUtils.copyProperties(principal,jwtToken);
        //HospitalUser user = this.findUserByName(jwtToken.getUsername(),false);
        return null;
    }
}
